﻿using CameraParentClassLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _01_多相机_框架
{
    public partial class CameraControl : UserControl
    {

        private CameraParent cameraParent = null;

        public CameraControl(CameraParent cameraParent)
        {
            InitializeComponent();

            this.cameraParent = cameraParent;
            //绑定委托可以调整等待操作为其他内容
            cameraParent.SetLabelText += Str => this.Invoke(new Action(() => { this.label2.Text = Str; }));
            cameraParent.isShowBtn += t => this.tableLayoutPanel2.Visible = t;
            //绑定响应事件
            cameraParent.OpenCameraEvent += () => OpenCameraSetButton();
            cameraParent.CloseCameraEvent += () => CloseCameraSetButton();
        }

        public void SetCameraName(string name)
        {
            cameraParent.CameraName = name;

            this.label1.Text = "[" + cameraParent.BrandName + "] " + cameraParent.CameraName;
        }

        public void OpenCameraSetButton()
        {
            this.button1.Enabled = false;
            this.button2.Enabled = true;
            this.button3.Enabled = true;
            this.button4.Enabled = true;
            this.label1.BackColor = Color.SeaGreen;
        }

        public void CloseCameraSetButton()
        {
            this.button1.Enabled = true;
            this.button2.Enabled = false;
            this.button3.Enabled = false;
            this.button4.Enabled = false;
            this.label1.BackColor = Color.Red;
        }
        /// <summary>
        /// 打开相机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            OpenCameraSetButton();
            cameraParent.OpenCamera();//打开相机
        }
        /// <summary>
        /// 单张采集
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            cameraParent.CollectOne();
        }
        /// <summary>
        /// 连续采集
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            cameraParent.CollectOneContinuous();
        }
        /// <summary>
        /// 关闭相机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            CloseCameraSetButton();//关闭相机
            cameraParent.CloseCamera();
        }
    }
}
